package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Customer;
import com.gxa.zhonglong.service.CustomerService;
import com.gxa.zhonglong.service.impl.CustomerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @auther JJuventus
 * @date 2021/10/26 9:17
 */

@RestController
@RequestMapping("/customer")
@Api(value = "客户资料")
@CrossOrigin
@Transactional(rollbackFor = Throwable.class)
public class CustomerController {

    @Autowired
    private CustomerService customerService = null;

    @RequestMapping("/getList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "want",value = "模糊查询的内容,无内容则为分页查询", dataType = "string"),
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "int")
    })
    public Map select(@RequestParam("want") String want,@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        List<Customer> customers = customerService.select(want, page, size);
        List<Customer> all = customerService.selectAll(want);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",all.size());
        jsonResult.setData(customers);
        return jsonResult.getDataMap();
    }

    @RequestMapping("/add")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "customer",value = "添加的客户", required = true,paramType = "body")
    )
    public Map add(@RequestBody Customer customer) throws BusinessException {
        customerService.add(customer);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @RequestMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId",value = "需要删除的客户的id号",required = true,dataType = "long"),
            @ApiImplicitParam(name = "customerPhone",value = "需要删除的客户的手机号",required = true,dataType = "string")
    })
    public Map delete(@RequestParam("customerId") Long customerId,@RequestParam("customerPhone") String customerPhone) throws BusinessException {
        customerService.delete(customerId, customerPhone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @RequestMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId",value = "需要删除的客户的id号",required = true,dataType = "long"),
            @ApiImplicitParam(name = "customerVipType",value = "会员类型", dataType = "string"),
            @ApiImplicitParam(name = "customerPhone",value = "联系电话",dataType = "string"),
    })
    public Map update(@RequestParam("customerId") Long customerId,@RequestParam("customerVipType") String customerVipType,@RequestParam("customerPhone") String customerPhone){
        customerService.update(customerId,customerVipType, customerPhone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
}
